﻿function DoView() {
    var type = $("#Type").val();
    if (type == -1)
        DoViewChuenVien();
    else if (type == -3)
        DoViewLichSo();
    else
        DoViewTruongPhong(type);
}

function DoViewTruongPhong(type) {
    var url = basePath + "LichLamViec/ViewLichAjax?id=" + type + "&&StrDate=" + $("#FromDate").val();
    $.getJSON(url, null, function (data) {
        var str = BuildTitleTable(type);
        for (var i = 0; i < data.length; i++) {
            str += BuildDataTable(data[i]);
        }
        //str += BuildBottomTable();
        $("#ViewLich").empty();
        $("#LichChuyenVien").empty();
        $("#ViewLich").append(str);
    });
    if (type == -2) {
        $("#menuaLichSo").removeAttr("class");
        $("#menuaTruongPhong").removeAttr("class");
        $("#menuaChuyenVien").removeAttr("class");
        $("#menuaLanhDaoSo").attr("class", "red");
    }
    else {
        $("#menuaLichSo").removeAttr("class");
        $("#menuaLanhDaoSo").removeAttr("class");
        $("#menuaChuyenVien").removeAttr("class");
        $("#menuaTruongPhong").attr("class", "red");
    }
    $("#Type").val(type);
    $("#IdDepartment").val("0");
}

function DoViewChuenVien() {
    var url = basePath + "LichLamViec/ViewLichAjax?id=-1&&StrDate=" + $("#FromDate").val();
    $.getJSON(url, null, function (data) {
        var str = "<table width='100%'><tr>";
        for (var i = 0; i < data.length; i++) {
            str += BuildDataTablePhongBan(data[i]);
        }
        str += "</tr></table>";
        $("#LichChuyenVien").empty();
        $("#LichChuyenVien").append(str);
        if ($("#IdDepartment").val() <= 0) {
            BuildTableChuyenVien(data[0][1]);
            $("#IdDepartment").val(data[0][1]);
            $("#menua" + data[0][1]).attr("class", "red");
        } else BuildTableChuyenVien($("#IdDepartment").val());
    });
    $("#menuaLichSo").removeAttr("class");
    $("#menuaLanhDaoSo").removeAttr("class");
    $("#menuaTruongPhong").removeAttr("class");
    $("#menuaChuyenVien").attr("class", "red");
    $("#Type").val(-1);
}

function BuildTableChuyenVien(id) {
    var url = basePath + "LichLamViec/ViewLichAjax?id=" + id + "&&StrDate=" + $("#FromDate").val();
    $.getJSON(url, null, function (data) {
        var str = BuildTitleTable();
        for (var i = 0; i < data.length; i++) {
            str += BuildDataTable(data[i]);
        }
        var oldDep_id = $("#IdDepartment").val();
        $("#menua" + oldDep_id).removeAttr("class");
        $("#menua" + id).attr("class", "red");
        $("#IdDepartment").val(id);
        $("#ViewLich").empty();
        $("#ViewLich").append(str);
    });
}
function BuildDataTablePhongBan(data) {
    var str = "<td style='border: 1px solid #ddd;cursor: pointer'><a onclick='BuildTableChuyenVien(" + data[1] + ")' id='menua" + data[1] + "'>" + data[0] + "</a></td>";
    return str;
}
function BuildDataTable(data) {
    var str = "<tr>"
                            + "    <td style='border: 1px solid #ddd' align='center' nowrap='nowrap'>"
                            + data[8]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            +  data[0]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            + data[7]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            + data[1]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            + data[2]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            + data[3]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            + data[4]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            + data[5]
                            + "    </td>"
                            + "    <td style='padding-left:3px;border: 1px solid #ddd'>"
                            + data[6]
                            + "    </td>"
                            + "</tr>";
    return str;
}
function BuildBottomTable() {
    var str = "<tr class='border_bottom'>"
                                + "<td  class='first' style='background: #DDDDDD;border: 1px solid #ddd'>"
                                + "</td>"
                                + "<td class='last' colspan='8' align='right' style='background: #DDDDDD; padding-right: 10px;border: 1px solid #ddd'>"
                                + "</td>"
                            + "</tr>"
                        + "</table>";
    return str;
}


function BuildTitleTable(type) {
    var str = "<table width='100%'  cellspacing='0' cellpadding='0' border='1' >"
                            + "<tr>"
                            + "    <td  style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>No </strong>"
                            + "    </td>"
                            + "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>Full Name</strong>"
                            + "    </td>";
    if (type != 0) {
        str += "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>JobTitles</strong>"
                            + "    </td>";
    }
    else {
        str += "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>Department</strong>"
                            + "    </td>";
    }
    str += "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>"
                            + "            MONDAY"
                            + "        </strong>"
                            + "    </td >"
                            + "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>"
                            + "            TUESDAY"
                            + "        </strong>"
                            + "    </td>"
                            + "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>"
                            + "            WEDNESDAY"
                            + "        </strong>"
                            + "    </td>"
                            + "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>"
                            + "            THURSDAY"
                            + "        </strong>"
                            + "    </td>"
                            + "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>"
                            + "            FRIDAY"
                            + "        </strong>"
                            + "    </td>"
                            + "    <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                            + "        <strong>"
                            + "            SATURDAY"
                            + "        </strong>"
                            + "    </td>"
                            + "</tr>";
    return str;
}

function DoViewLichSo() {
    var strHead = "<table width='100%'style='height: 350px;' cellpadding='0' cellspacing='0' border='1' class='data display datatable'>"
                  + "      <tr>"
                  + "          <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                  + "              <strong>"
                  + "                  Date"
                  + "              </strong>"
                  + "          </td>"
                  + "          <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                  + "              <strong>"
                  + "                  Time"
                  + "              </strong>"
                  + "          </td>"
                  + "          <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                  + "              <strong>"
                  + "                 Contents"
                  + "              </strong>"
                  + "          </td>"
                  + "          <td style='border: 1px solid #ddd 'nowrap='nowrap' align='center'>"
                  + "              <strong>"
                  + "                  Division"
                  + "              </strong>"
                  + "          </td>"
                  + "      </tr>";
    var foodter = "</table>";
    var middle = "";
    var url = basePath + "LichLamViec/ViewLichAjax?id=-3&&StrDate=" + $("#FromDate").val();
    $.getJSON(url, null, function (data) {
        $("#LichChuyenVien").empty();
        $("#ViewLich").empty();
        $("#ViewLich").append(strHead + data + foodter);
    });

    $("#Type").val(-3);
    $("#menuaTruongPhong").removeAttr("class");
    $("#menuaChuyenVien").removeAttr("class");
    $("#menuaLanhDaoSo").removeAttr("class");
    $("#menuaLichSo").attr("class", "red");
}

function XemLich(idLich) {
    window.location.href = basePath + 'LichLamViec/XemLichLamViec/' + idLich;
}

//phần cho lightbox
//$(document).ready(function () {
//    $(".XemThanhPhan").click(function () {
//        var id = this.id.replace("XemThanhPhan", "");
//        $("#tbl_DanhSachThanhPhan>table>tbody").empty();
//        var url = basePath + 'LichLamViec/ViewDetailThanhPhan?IdLich=' + id;
//        $.getJSON(url, null, function (data) {
//            for (var i = 0; i < data.length; i++) {
//                $("#tbl_DanhSachThanhPhan>table>tbody").append("<tr>" +
//                                                            "<td style='width: 30px;text-align:right'><img src='../../Content/ImagesAdmin/check.PNG'/></td>" +
//                                                            "<td>" + data[i] + "</td>" +
//                                                       "</tr>");
//            }
//        });
//        var p = $(this).position();
//        $("#ViewThanhPhan").css({ "top": +(p.top) + "px", "left": +(p.left - 270) + "px", "display": "block" });
//    });
//});


function XemChiTietThanhPhan(id) {
    $("#tbl_DanhSachThanhPhan>table>tbody").empty();
    var url = basePath + 'LichLamViec/ViewDetailThanhPhan?IdLich=' + id;
    $.getJSON(url, null, function (data) {
        for (var i = 0; i < data.length; i++) {
            $("#tbl_DanhSachThanhPhan>table>tbody").append("<tr>" +
                                                            "<td style='width: 30px;text-align:right;'><img src='../../Content/img/check.PNG'/></td>" +
                                                            "<td>" + data[i] + "</td>" +
                                                       "</tr>");
        }
    });
    var p = $("#XemThanhPhan" + id).position();
    $("#ViewThanhPhan").css({ "top": +(p.top) + "px", "left": +(p.left - 270) + "px", "display": "block" });
}

String.prototype.replaceAll = function (
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString)

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
}